package com.lg.lock;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;

import java.util.concurrent.CountDownLatch;

/**
 * zk 的 watcher
 *
 * @author by Mr. Li 2020/12/10 15:23
 */
public class ZooKeeperWatcher implements Watcher {
    private static CountDownLatch connectedSemaphoreCountDownLatch = new CountDownLatch(1);
    @Override
    public void process(WatchedEvent event) {
        System.out.println("Receive watched event: " + event.getState());

        if (Event.KeeperState.SyncConnected == event.getState()) { // 判断节点状态是否连接成功
            connectedSemaphoreCountDownLatch.countDown();
        }

    }
}
